package com.gyc.game.cchess.model;

import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_BISHOP;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_CANON;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_CAR;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_ELEPHANT;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_HORSE;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_KING;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_PAWN;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.IsRed;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.IsSameSide;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.NOCHESS;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_BISHOP;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_CANON;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_CAR;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_ELEPHANT;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_HORSE;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_KING;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_PAWN;

import java.util.Arrays;

public class Eveluator {

	// 10 9
	public static int[] BA0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 90, 110, 120,
			120, 120, 110, 90, 90, 90, 90, 110, 120, 120, 120, 110, 90, 90, 70,
			90, 110, 110, 110, 110, 110, 90, 70, 70, 70, 70, 70, 70, 70, 70,
			70, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0 };
	// 10 9
	public static int[] BA1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 90,
			110, 110, 110, 110, 110, 90, 70, 90, 90, 110, 120, 120, 120, 110,
			90, 90, 90, 90, 110, 120, 120, 120, 110, 90, 90, 0, 0, 0, 0, 0, 0,
			0, 0, 0 };

	public static final int BASEVALUE_BISHOP = 250;

	public static final int BASEVALUE_CANON = 350;

	public static final int BASEVALUE_CAR = 500;
	public static final int BASEVALUE_ELEPHANT = 250;

	public static final int BASEVALUE_HORSE = 350;

	public static final int BASEVALUE_KING = 10000;
	public static final int BASEVALUE_PAWN = 100;

	public static final int FLEXIBILITY_BISHOP = 1;
	public static final int FLEXIBILITY_CANON = 6;
	public static final int FLEXIBILITY_CAR = 6;
	public static final int FLEXIBILITY_ELEPHANT = 1;

	public static final int FLEXIBILITY_HORSE = 12;

	public static final int FLEXIBILITY_KING = 0;
	public static final int FLEXIBILITY_PAWN = 15;

	public static int count = 0;

	short[] m_AttackPos;// 10*9

	byte[] m_FlexibilityPos;// 10*9

	// byte[][] m_GuardPos;// 10*9
	byte[] m_GuardPos;// 10*9

	// int[][] m_chessValue;// 10*9

	int[] m_chessValue;// 10*9

	int[] m_BaseValue;// 255
	int[] m_FlexValue;// 15

	int nPosCount;

	Point RelatePos[];// 20

	public Eveluator() {
		super();
		m_AttackPos = new short[90];// 10*9
		m_chessValue = new int[90];// 10*9
		m_FlexibilityPos = new byte[90];// 10*9
		m_GuardPos = new byte[90];// 10*9

		m_BaseValue = new int[128]; // 最大 112 0x70
		m_FlexValue = new int[128];

		RelatePos = new Point[20];

		for (int i = 0; i < RelatePos.length; i++) {
			RelatePos[i] = new Point();
		}

		m_BaseValue[B_KING] = BASEVALUE_KING;
		m_BaseValue[B_CAR] = BASEVALUE_CAR;
		m_BaseValue[B_HORSE] = BASEVALUE_HORSE;
		m_BaseValue[B_BISHOP] = BASEVALUE_BISHOP;
		m_BaseValue[B_ELEPHANT] = BASEVALUE_ELEPHANT;
		m_BaseValue[B_CANON] = BASEVALUE_CANON;
		m_BaseValue[B_PAWN] = BASEVALUE_PAWN;

		m_BaseValue[R_KING] = BASEVALUE_KING;
		m_BaseValue[R_CAR] = BASEVALUE_CAR;
		m_BaseValue[R_HORSE] = BASEVALUE_HORSE;
		m_BaseValue[R_BISHOP] = BASEVALUE_BISHOP;
		m_BaseValue[R_ELEPHANT] = BASEVALUE_ELEPHANT;
		m_BaseValue[R_CANON] = BASEVALUE_CANON;
		m_BaseValue[R_PAWN] = BASEVALUE_PAWN;

		m_FlexValue[B_KING] = FLEXIBILITY_KING;
		m_FlexValue[B_CAR] = FLEXIBILITY_CAR;
		m_FlexValue[B_HORSE] = FLEXIBILITY_HORSE;
		m_FlexValue[B_BISHOP] = FLEXIBILITY_BISHOP;
		m_FlexValue[B_ELEPHANT] = FLEXIBILITY_ELEPHANT;
		m_FlexValue[B_CANON] = FLEXIBILITY_CANON;
		m_FlexValue[B_PAWN] = FLEXIBILITY_PAWN;

		m_FlexValue[R_KING] = FLEXIBILITY_KING;
		m_FlexValue[R_CAR] = FLEXIBILITY_CAR;
		m_FlexValue[R_HORSE] = FLEXIBILITY_HORSE;
		m_FlexValue[R_BISHOP] = FLEXIBILITY_BISHOP;
		m_FlexValue[R_ELEPHANT] = FLEXIBILITY_ELEPHANT;
		m_FlexValue[R_CANON] = FLEXIBILITY_CANON;
		m_FlexValue[R_PAWN] = FLEXIBILITY_PAWN;

	}

	void AddPoint(int xy) {
		RelatePos[nPosCount].xy = xy;
		nPosCount++;
	}

	void AddPoint(int x, int y) {
		RelatePos[nPosCount].xy = y * 8 + y + x;
		nPosCount++;
	}

	public static void memset(int[][] value, int content) {
		int vl = value.length;
		for (int i = 0; i < vl; i++) {
			Arrays.fill(value[i], content);
		}
	}

	public static void memset(short[][] value, int content) {
		int vl = value.length;
		short content2 = (short) content;

		for (int i = 0; i < vl; i++) {
			Arrays.fill(value[i], content2);
		}

	}

	public static void memset(byte[][] value, int content) {
		byte content2 = (byte) content;
		int vl = value.length;
		for (int i = 0; i < vl; i++) {
			Arrays.fill(value[i], content2);
		}
	}

	byte npci;

	// byte evaluateChessId;
	byte eci;

	// evaluateTargetChessId;;
	byte nTargetType;

	int nHalfvalue;

	int eveluate(ChessBoardBitPlanes position, boolean isRed) {

		count++;

		Arrays.fill(m_chessValue, 0);
		Arrays.fill(m_AttackPos, (short) 0);
		Arrays.fill(m_GuardPos, (byte) 0);
		Arrays.fill(m_FlexibilityPos, (byte) 0);

		// byte nTargetType;
		for (byte xy = 0; xy < BitPlane.MAX; xy++) {

			eci = position.bp_all.getXY(xy);
			if (eci != NOCHESS) {
				GetRelatePiece(position, xy, eci);

				for (npci = 0; npci < nPosCount; npci++) {

					final int rpxy4npci = RelatePos[npci].xy;
					nTargetType = position.getXY((byte) rpxy4npci);

					// 目标棋子为空,可移动值增加
					if (nTargetType == NOCHESS) {
						m_FlexibilityPos[xy]++;
					} else { // 目标棋子不为空
						if (IsSameSide(eci, nTargetType)) {
							// 相同颜色 那么目标位置棋子 受保护
							m_GuardPos[rpxy4npci]++;
						} else {
							// 相反颜色 那么目标位置棋子 受攻击
							m_AttackPos[rpxy4npci]++;
							m_FlexibilityPos[xy]++;

							// 被攻击的棋子
							switch (nTargetType) {
							case R_KING:
								if (!isRed) {
									return 18888;
								}
								break;
							case B_KING:
								if (isRed) {
									return 18888;
								}
								break;
							default:
								m_AttackPos[rpxy4npci] += 3 + (m_BaseValue[nTargetType] - m_BaseValue[eci]) / 100;
								break;
							}
						}
					}
				}
			}
		}

		for (byte xy = 0; xy < BitPlane.MAX; xy++) {
			final byte chessId = position.bp_all.getXY(xy);
			if (chessId != NOCHESS) {

				m_chessValue[xy]++;
				m_chessValue[xy] += m_FlexValue[chessId] * m_FlexibilityPos[xy];

				if (chessId == R_PAWN) {
					m_chessValue[xy] += BA0[xy];
				} else if (chessId == B_PAWN) {
					m_chessValue[xy] += BA1[xy];
				}

			}
		}

		for (byte xy = 0; xy < BitPlane.MAX; xy++) {
			final byte chessId = position.bp_all.getXY(xy);

			if (chessId != NOCHESS) {
				nHalfvalue = m_BaseValue[chessId] / 16;
				m_chessValue[xy] += m_BaseValue[chessId];

				if (IsRed(chessId)) {// 红子
					if (m_AttackPos[xy] != 0) {
						if (isRed) {
							if (chessId == R_KING) {
								m_chessValue[xy] -= 20;
							} else {
								m_chessValue[xy] -= nHalfvalue + nHalfvalue;
								if (m_GuardPos[xy] != 0)
									m_chessValue[xy] += nHalfvalue;
							}
						} else {
							if (chessId == R_KING) {
								return 18888;
							}
							m_chessValue[xy] -= nHalfvalue * 10;
							if (m_GuardPos[xy] != 0)
								m_chessValue[xy] += nHalfvalue * 8 + nHalfvalue;
						}
						m_chessValue[xy] -= m_AttackPos[xy];
					} else {
						if (m_GuardPos[xy] != 0)
							m_chessValue[xy] += 5;
					}
				} else {// 黑子
					if (m_AttackPos[xy] != 0) {
						if (!isRed) {
							if (chessId == B_KING) {
								m_chessValue[xy] -= 20;
							} else {
								m_chessValue[xy] -= nHalfvalue + nHalfvalue;
								if (m_GuardPos[xy] != 0)
									m_chessValue[xy] += nHalfvalue;
							}
						} else {
							if (chessId == B_KING) {
								return 18888;
							}
							m_chessValue[xy] -= nHalfvalue * 10;
							if (m_GuardPos[xy] != 0)
								m_chessValue[xy] += nHalfvalue * 8 + nHalfvalue;
						}
						m_chessValue[xy] -= m_AttackPos[xy];
					} else {
						if (m_GuardPos[xy] != 0)
							m_chessValue[xy] += 5;
					}
				}

			}
		}

		int nRedValue = 0;
		int nBlackValue = 0;

		for (byte xy = 0; xy < BitPlane.MAX; xy++) {
			byte nChessType = position.getXY(xy);
			if (nChessType != NOCHESS) {
				if (IsRed(nChessType)) {
					nRedValue += m_chessValue[xy];
				} else {
					nBlackValue += m_chessValue[xy];
				}
			}
		}

		if (isRed) {
			return nRedValue - nBlackValue;
		} else {
			return nBlackValue - nRedValue;
		}
	}

	boolean blocked;

	boolean flag;

	int tcolumn, trow;
	byte oxy = -1;

	int GetRelatePiece(ChessBoardBitPlanes position, byte rowcolumn,
			byte xyChessId)// 10*9
	{
		nPosCount = 0;

		switch (xyChessId) {
		case R_KING:
			// 66 67 68
			// 75 76 77
			// 84 85 86
			switch (rowcolumn) {
			case 66:
				AddPoint(67);
				AddPoint(75);
				break;
			case 67:
				AddPoint(66);
				AddPoint(76);
				AddPoint(68);
				break;
			case 68:
				AddPoint(67);
				AddPoint(77);
				break;
			case 75:
				AddPoint(66);
				AddPoint(76);
				AddPoint(84);
				break;
			case 76:
				AddPoint(67);
				AddPoint(75);
				AddPoint(77);
				AddPoint(85);
				break;
			case 77:
				AddPoint(68);
				AddPoint(76);
				AddPoint(86);
				break;
			case 84:
				AddPoint(75);
				AddPoint(85);
				break;
			case 85:
				AddPoint(84);
				AddPoint(76);
				AddPoint(86);
				break;
			case 86:
				AddPoint(85);
				AddPoint(77);
				break;
			default:
				break;
			}

			oxy = -1;
			for (int i = 0; i < ChessBoardBitPlanes.Pos_B_KING.length; i++) {
				byte pos = ChessBoardBitPlanes.Pos_B_KING[i];
				if (position.bp_all.getXY(pos) == B_KING) {
					oxy = pos;
					break;
				}
			}

			if (oxy > -1) {
				if ((rowcolumn - oxy) % 9 == 0) {
					blocked = false;
					for (int i = oxy + 9; i < rowcolumn; i += 9) {
						if (position.bp_all.getXY(i) != NOCHESS) {
							blocked = true;
							break;
						}
					}
					if (!blocked) {
						AddPoint(oxy);
					}
				}
			}

			break;
		case B_KING:
			// 03 04 05
			// 12 13 14
			// 21 22 23
			switch (rowcolumn) {
			case 3:
				AddPoint(4);
				AddPoint(12);
				break;
			case 4:
				AddPoint(3);
				AddPoint(5);
				AddPoint(13);
				break;
			case 5:
				AddPoint(4);
				AddPoint(14);
				break;
			case 12:
				AddPoint(3);
				AddPoint(13);
				AddPoint(21);
				break;
			case 13:
				AddPoint(4);
				AddPoint(14);
				AddPoint(12);
				AddPoint(22);
				break;
			case 14:
				AddPoint(5);
				AddPoint(13);
				AddPoint(23);
				break;
			case 21:
				AddPoint(12);
				AddPoint(22);
				break;
			case 22:
				AddPoint(21);
				AddPoint(23);
				AddPoint(13);
				break;
			case 23:
				AddPoint(14);
				AddPoint(22);
				break;
			default:
				break;
			}

			oxy = -1;
			for (int i = 0; i < ChessBoardBitPlanes.Pos_R_KING.length; i++) {
				byte pos = ChessBoardBitPlanes.Pos_R_KING[i];
				if (position.bp_all.getXY(pos) == R_KING) {
					oxy = pos;
					break;
				}
			}
			if (oxy > -1) {
				if ((rowcolumn - oxy) % 9 == 0) {
					blocked = false;
					for (int i = oxy + 9; i < rowcolumn; i += 9) {
						if (position.bp_all.getXY(i) != NOCHESS) {
							blocked = true;
							break;
						}
					}

					if (!blocked) {
						AddPoint(oxy);
					}
				}
			}
			break;

		case R_BISHOP:
			switch (rowcolumn) {
			case 84:
				AddPoint(76);
				break;
			case 86:
				AddPoint(76);
				break;
			case 66:
				AddPoint(76);
				break;
			case 68:
				AddPoint(76);
				break;
			case 76:
				AddPoint(84);
				AddPoint(86);
				AddPoint(66);
				AddPoint(68);
				break;
			default:
				break;
			}

			break;

		case B_BISHOP:
			switch (rowcolumn) {
			case 3:
				AddPoint(13);
				break;
			case 5:
				AddPoint(13);
				break;
			case 21:
				AddPoint(13);
				break;
			case 23:
				AddPoint(13);
				break;
			case 13:
				AddPoint(3);
				AddPoint(5);
				AddPoint(21);
				AddPoint(23);
				break;
			default:
				break;
			}

			break;

		case R_ELEPHANT:
			// ______47______51
			// ____55__57__59___61
			// __63______67_______71
			// ____73__75__77___79
			// ______83______87

			switch (rowcolumn) {
			case 47:
				if (position.bp_all.getXY(55) == NOCHESS)
					AddPoint(63);
				if (position.bp_all.getXY(57) == NOCHESS)
					AddPoint(67);
				break;
			case 51:
				if (position.bp_all.getXY(59) == NOCHESS)
					AddPoint(67);
				if (position.bp_all.getXY(61) == NOCHESS)
					AddPoint(71);
				break;
			case 63:
				if (position.bp_all.getXY(55) == NOCHESS)
					AddPoint(47);
				if (position.bp_all.getXY(73) == NOCHESS)
					AddPoint(83);
				break;
			case 67:
				if (position.bp_all.getXY(57) == NOCHESS)
					AddPoint(47);
				if (position.bp_all.getXY(59) == NOCHESS)
					AddPoint(51);
				if (position.bp_all.getXY(75) == NOCHESS)
					AddPoint(83);
				if (position.bp_all.getXY(77) == NOCHESS)
					AddPoint(87);

				break;
			case 71:
				if (position.bp_all.getXY(61) == NOCHESS)
					AddPoint(51);
				if (position.bp_all.getXY(79) == NOCHESS)
					AddPoint(87);
				break;
			case 83:
				if (position.bp_all.getXY(73) == NOCHESS)
					AddPoint(63);
				if (position.bp_all.getXY(75) == NOCHESS)
					AddPoint(67);
				break;
			case 87:
				if (position.bp_all.getXY(77) == NOCHESS)
					AddPoint(67);
				if (position.bp_all.getXY(79) == NOCHESS)
					AddPoint(71);
				break;
			default:
				break;
			}

			break;
		case B_ELEPHANT:
			// __2_ 6
			// 18 22 26
			// _38 42

			switch (rowcolumn) {
			case 2:
				if (position.bp_all.getXY(10) == NOCHESS) {
					AddPoint(18);
				}
				if (position.bp_all.getXY(12) == NOCHESS) {
					AddPoint(22);
				}
				break;
			case 6:
				if (position.bp_all.getXY(16) == NOCHESS) {
					AddPoint(26);
				}
				if (position.bp_all.getXY(14) == NOCHESS) {
					AddPoint(22);
				}
				break;
			case 18:
				if (position.bp_all.getXY(10) == NOCHESS) {
					AddPoint(2);
				}
				if (position.bp_all.getXY(28) == NOCHESS) {
					AddPoint(38);
				}
				break;
			case 22:
				if (position.bp_all.getXY(12) == NOCHESS) {
					AddPoint(2);
				}
				if (position.bp_all.getXY(14) == NOCHESS) {
					AddPoint(6);
				}

				if (position.bp_all.getXY(30) == NOCHESS) {
					AddPoint(38);
				}
				if (position.bp_all.getXY(32) == NOCHESS) {
					AddPoint(42);
				}

				break;
			case 26:
				if (position.bp_all.getXY(16) == NOCHESS) {
					AddPoint(6);
				}
				if (position.bp_all.getXY(34) == NOCHESS) {
					AddPoint(42);
				}
				break;
			case 38:
				if (position.bp_all.getXY(28) == NOCHESS) {
					AddPoint(18);
				}
				if (position.bp_all.getXY(30) == NOCHESS) {
					AddPoint(22);
				}

				break;
			case 42:
				if (position.bp_all.getXY(32) == NOCHESS) {
					AddPoint(22);
				}
				if (position.bp_all.getXY(34) == NOCHESS) {
					AddPoint(26);
				}

				break;
			default:
				break;
			}
			break;

		case R_HORSE:
		case B_HORSE:
			int row = rowcolumn / 9;
			int column = rowcolumn % 9;

			if ((column < 7 && row < 9)
					&& position.getXY((byte) (rowcolumn + 1)) == NOCHESS)
				AddPoint(rowcolumn + 11);

			if ((column < 7 && row >= 1)
					&& position.getXY((byte) (rowcolumn + 1)) == NOCHESS)
				AddPoint(rowcolumn - 7);

			if ((column >= 2 && row < 9)
					&& position.getXY((byte) (rowcolumn - 1)) == NOCHESS)
				AddPoint(rowcolumn + 7);

			if ((column >= 2 && row > 1)
					&& position.getXY((byte) (rowcolumn - 1)) == NOCHESS)
				AddPoint(rowcolumn - 11);

			if ((column < 8 && row < 8)
					&& position.getXY((byte) (rowcolumn + 9)) == NOCHESS)
				AddPoint(rowcolumn + 19);

			if ((column >= 1 && row < 8)
					&& position.getXY((byte) (rowcolumn + 9)) == NOCHESS)
				AddPoint(rowcolumn + 17);

			if ((column < 8 && row >= 2)
					&& position.getXY((byte) (rowcolumn - 9)) == NOCHESS)
				AddPoint(rowcolumn - 17);

			if ((column >= 1 && row >= 2)
					&& position.getXY((byte) (rowcolumn - 9)) == NOCHESS)
				AddPoint(rowcolumn - 19);
			break;

		case R_CAR:
		case B_CAR:

			row = rowcolumn / 9;
			column = rowcolumn % 9;

			tcolumn = column + 1;
			trow = row;

			while (tcolumn < 9) {
				if (NOCHESS == position.getXY(trow, tcolumn))
					AddPoint(tcolumn, trow);
				else {
					AddPoint(tcolumn, trow);
					break;
				}
				tcolumn++;
			}

			tcolumn = column - 1;
			trow = row;
			while (tcolumn >= 0) {
				if (NOCHESS == position.getXY(trow, tcolumn))
					AddPoint(tcolumn, trow);
				else {
					AddPoint(tcolumn, trow);
					break;
				}
				tcolumn--;
			}

			tcolumn = column;
			trow = row + 1;//
			while (trow < 10) {
				if (NOCHESS == position.getXY(trow, tcolumn))
					AddPoint(tcolumn, trow);
				else {
					AddPoint(tcolumn, trow);
					break;
				}
				trow++;
			}

			tcolumn = column;
			trow = row - 1;
			while (trow >= 0) {
				if (NOCHESS == position.getXY(trow, tcolumn))
					AddPoint(tcolumn, trow);
				else {
					AddPoint(tcolumn, trow);
					break;
				}
				trow--;
			}
			break;

		case R_PAWN:

			row = rowcolumn / 9;

			if (row >= 1)
				AddPoint(rowcolumn - 9);
			if (row < 5) {
				column = rowcolumn % 9;
				if (column < 8)
					AddPoint(rowcolumn + 1);
				if (column >= 1)
					AddPoint(rowcolumn - 1);
			}
			break;

		case B_PAWN:

			row = rowcolumn / 9;
			if (row < 8)
				AddPoint(rowcolumn + 9);
			if (row > 4) {
				column = rowcolumn % 9;
				if (column < 8)
					AddPoint(rowcolumn + 1);
				if (column >= 1)
					AddPoint(rowcolumn - 1);
			}
			break;

		case B_CANON:
		case R_CANON:

			row = rowcolumn / 9;
			column = rowcolumn % 9;

			tcolumn = column + 1; // 
			trow = row;
			flag = false;

			while (tcolumn < 9) {
				if (NOCHESS == position.getXY(trow, tcolumn)) {
					if (!flag) {
						AddPoint(tcolumn, trow);
					}
				} else {
					if (flag) {
						AddPoint(tcolumn, trow);
						break;
					} else {
						flag = true;
					}
				}
				tcolumn++;
			}

			tcolumn = column - 1;
			flag = false;
			while (tcolumn >= 0) {
				if (NOCHESS == position.getXY(trow, tcolumn)) {
					if (!flag) {
						AddPoint(tcolumn, trow);
					}
				} else {
					if (flag) {
						AddPoint(tcolumn, trow);
						break;
					} else {
						flag = true;
					}
				}
				tcolumn--;
			}
			tcolumn = column;
			trow = row + 1;
			flag = false;
			while (trow < 10) {
				if (NOCHESS == position.getXY(trow, tcolumn)) {
					if (!flag) {
						AddPoint(tcolumn, trow);
					}
				} else {
					if (flag) {
						AddPoint(tcolumn, trow);
						break;
					} else {
						flag = true;
					}
				}
				trow++;
			}

			trow = row - 1; // 
			flag = false;
			while (trow >= 0) {
				if (NOCHESS == position.getXY(trow, tcolumn)) {
					if (!flag) {
						AddPoint(tcolumn, trow);
					}
				} else {
					if (flag) {
						AddPoint(tcolumn, trow);
						break;
					} else {
						flag = true;
					}
				}
				trow--;
			}
			break;

		default:
			break;

		}
		return nPosCount;
	}

}
